#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;

[InitializeOnLoad]
public static class SafeDestroyQueue
{
    private static readonly List<GameObject> destroyQueue = new();

    static SafeDestroyQueue()
    {
        EditorApplication.update += ProcessDestroyQueue;
    }

    private static void ProcessDestroyQueue()
    {
        if (EditorApplication.isPlaying) return;

        foreach (var go in destroyQueue)
        {
            if (go != null)
                Object.DestroyImmediate(go);
        }

        destroyQueue.Clear();
    }

    public static void Destroy(GameObject go)
    {
        if (go != null && !destroyQueue.Contains(go))
            destroyQueue.Add(go);
    }
}
#endif